package com.ttbj.move_rule.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ttbj.core.util.CurrencyUtils;
import com.ttbj.move_rule.dao.BillMoveMapper;
import com.ttbj.move_rule.dao.BillPieceMapper;
import com.ttbj.move_rule.dao.BillSecondMapper;
import com.ttbj.move_rule.dao.BillServeMapper;
import com.ttbj.move_rule.dao.BillVehicleMapper;
import com.ttbj.move_rule.po.BillDistance;
import com.ttbj.move_rule.po.BillMove;
import com.ttbj.move_rule.po.BillServe;
import com.ttbj.move_rule.po.BillVehicle;
import com.ttbj.move_rule.service.MoveBillService;
import com.ttbj.rule.service.WtBillService;

@Service
public class MoveBillServiceImpl implements MoveBillService {

	private static final Logger logger = LoggerFactory.getLogger(MoveBillServiceImpl.class);

	private static final int DISTANCE_RULE = 1;
	private static final int PIECE_RULE = 2;
	private static final int TIME_RULE = 3;

	@Autowired
	private DisMoveBillStrategy disMoveBillStrategy = null;

	@Autowired
	private PieceMoveBillStrategy pieceMoveBillStrategy = null;

	@Autowired
	private TimeMoveBillStrategy timeMoveBillStrategy = null;

	@Autowired
	private BillServeMapper billServeMapper = null; // 服务权重mapper

	@Autowired
	private BillMoveMapper billMoveMapper = null;
	
	@Autowired
	private WtBillService wtBillService = null;
	

	@Override
	public Float getFee(Integer billType, String senderLat, String senderLng, String reciverLat, String reciverLng, Integer floorNum, Integer pieceNum, Integer baleNum, String truckType, Integer serciveLevel, Integer orderDriverRequire) throws Exception {
		billType = null == billType ? 3 : billType;
		orderDriverRequire = null == orderDriverRequire ? 1 : orderDriverRequire;
		Float fee = 0f;
		switch (billType) {
		case DISTANCE_RULE:// 距离
			fee = disMoveBillStrategy.strategyInterface(billType, senderLat, senderLng, reciverLat, reciverLng, floorNum, pieceNum, baleNum, truckType, serciveLevel, orderDriverRequire);
			break;
		case PIECE_RULE:// 按件
			fee = pieceMoveBillStrategy.strategyInterface(billType, senderLat, senderLng, reciverLat, reciverLng, floorNum, pieceNum, baleNum, truckType, serciveLevel, orderDriverRequire);
			break;
		case TIME_RULE:// 按次
			fee = timeMoveBillStrategy.strategyInterface(billType, senderLat, senderLng, reciverLat, reciverLng, floorNum, pieceNum, baleNum, truckType, serciveLevel, orderDriverRequire);
			break;
		default:
			break;
		}
		Float wt = getWt(serciveLevel); //  服务权重和昼夜权重
		fee = wt * fee;
		logger.info("搬家的总费用:{}元",fee);
		fee = CurrencyUtils.toAfterTwo(fee);
		logger.info("搬家的保留后二位总费用:{}元",fee);
		return fee;
	}

	/**
	 * 获取权重
	 * 
	 * @throws Exception
	 */
	private Float getWt(Integer serciveLevel) throws Exception {
		Float wtService = weightServe(serciveLevel); //服务权重
		logger.info("服务权重:{}",wtService);
		Float wtTiming = wtBillService.getWtTiming(1);//昼夜权重
		logger.info("昼夜权重:{}",wtTiming);
		Float wt  = wtService * wtTiming;
		return  wt.equals(0f)||null == wt ? 1f : wt;
	}

	/**
	 * 服务权重
	 * 
	 * @param truckType
	 * @param serciveLevel
	 * @param disance
	 * @param distanceRule
	 * @throws Exception
	 */
	private Float weightServe(Integer serciveLevel) throws Exception {
		Float weight = null;
		BillServe bill = billServeMapper.selectByPrimaryKey(1);
		switch (serciveLevel) {
		case 1:
			weight = bill.getServeOrdinary(); // 普通搬家
			break;
		case 2:
			weight = bill.getServeSmallPiece();// 长途搬家
			break;
		case 3:
			weight = bill.getServeBoutique();// 精品搬家
			break;
		case 4:
			weight = bill.getServeOffice(); // 办公室搬家
			break;
		case 5:
			break;
		default:
			break;
		}
		weight = null == weight ? 1f : weight;
		return weight;
	}
	
	
 
	@Override
	public Float carryFee(Integer floorNum) {
		Float moveUnit = getBillMove(2);// 封装获取搬家附加项目的单价
		moveUnit = null == moveUnit ? 1 : moveUnit;
		floorNum = null == floorNum ? 1 : floorNum;
		return moveUnit * floorNum;
	}

	@Override
	public Float packFee(Integer baleNum) {
		Float packUnit = getBillMove(1);// 封装获取搬家附加项目的单价
		packUnit = null == packUnit ? 0f : packUnit;
		baleNum = null == baleNum ? 0 : baleNum;
		return packUnit * baleNum;
	}

	/**
	 * 封装获取搬家附加项目的单价
	 * 
	 * @param flag
	 * @return
	 */
	private Float getBillMove(Integer flag) {
		BillMove bill = billMoveMapper.selectByPrimaryKey(1l);
		Float unit = null;
		if (null == bill) {
			return 0f;
		}
		switch (flag) {
		case 1:
			Float packUnit = bill.getMovePackUnit();
			unit = null == packUnit ? 0f : packUnit;
			break;
		case 2:
			Float moveUnit = bill.getMoveCarryUnit();
			unit = null == moveUnit ? 0f : moveUnit;
			break;

		default:
			unit = 0f;
			break;
		}
		return unit;

	}

}
